package com.avantis.admin.service.impl;

import com.avantis.admin.service.WareHouseService;
import com.avantis.common.base.BaseServiceImpl;
import com.avantis.common.entity.WareHouse;
import com.avantis.common.exception.ServiceProcessException;
import com.avantis.common.repository.WareHouseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
* @Description:    java类作用描述
* @Author:         kirago
* @CreateDate:     2019/1/3 10:40 AM
* @UpdateRemark:   修改内容
* @Version:        1.0
*/

@Service
@Transactional
public class WareHouseServiceImpl extends BaseServiceImpl<WareHouseRepository, WareHouse> implements WareHouseService {

    @Autowired
    private WareHouseService wareHouseService;

    @Autowired
    private WareHouseRepository wareHouseRepository;

    @Override
    public boolean checkByWarehouseCode(String warehousecode) {
        WareHouse wareHouse = repository.findByWarehouseCode(warehousecode);
        if(wareHouse == null) return true;
        return false;
    }


    @Override
    public void saveWarehouse(WareHouse wareHouse) {
        try {
            save(wareHouse);
        } catch (ServiceProcessException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceProcessException("新增仓库失败!");
        }

    }

    @Override
    public List<WareHouse> findAll() {
        return repository.findAll();
    }

    @Override
    public WareHouse findOne(Integer id) {
        Optional<WareHouse> wareHouseOptional = repository.findById(id);
        if (wareHouseOptional.isPresent()) {
            return wareHouseOptional.get();
        }else {
            return null;
        }
    }

    @Override
    public void deleteById(Integer integer) {
        try{
            repository.deleteById(integer);
        } catch (ServiceProcessException e){
            throw e;
        } catch (Exception e) {
            throw new ServiceProcessException("删除仓库失败！");
        }
    }

}

